<template>
  <van-row class="main-menu">
    <van-nav-bar left-text="返回" title="各单位问题整改跟踪" @click-left="doBack"/>

    <van-cell title="选择日期区间" :value="date" @click="show = true" />
    <van-calendar v-model="show" type="range" @confirm="onConfirm" :min-date="minDate" :max-date="maxDate"/>
    <van-cell-group>
        <van-cell title="非公司层面问题整改"  />
    </van-cell-group>
    <van-row>
      <van-empty v-if="showEmptyUncompany" description="无分析数据" />
      <dept-rectify-uncompany-monitor v-if="showEmptyUncompany === false" ref = 'uncompanyMonitor' :chartData='uncompanyChartData' :tableData='uncompanyTableData' />
    </van-row>
    <van-cell-group>
        <van-cell title="公司层面问题整改" />
    </van-cell-group>
    <van-row>
      <van-empty v-if="showEmptyCompany" description="无分析数据" />
      <dept-rectify-company-monitor v-if="showEmptyCompany === false" ref = 'companyMonitor' :chartData='companyChartData' :tableData='companyTableData' />
    </van-row>
    <van-cell-group>
        <van-cell title="整改完成率和及时率分析"  />
    </van-cell-group>
    <van-cell-group title="总体整改完成率和及时率分析">
        <van-cell title="公司层面问题完成率" :value="companyIntimeWancheng"/>
        <van-cell title="公司层面问题及时率" :value="companyIntimeJishi"/>
        <van-cell title="非公司层面问题完成率" :value="uncompanyIntimeWancheng"/>
        <van-cell title="非公司层面问题及时率" :value="uncompanyIntimeJishi"/>
    </van-cell-group>
    <van-cell-group>
        <van-cell title="各部门非公司层面问题整改完成率和及时率分析"  />
    </van-cell-group>
    <van-row>
      <van-empty v-if="showEmptyUnCompanyIntime" description="无分析数据" />
      <dept-rectify-intime-analysis v-if="showEmptyUnCompanyIntime === false" ref = 'uncompanyIntimeAnalysis' chartdomid='uncompanyIntimeChart' :intimeDetail='uncompanyIntimeDataDetail' />
    </van-row>
    <van-cell-group>
        <van-cell title="各部门公司层面问题整改完成率和及时率分析"  />
    </van-cell-group>
    <van-row>
      <van-empty v-if="showEmptyCompanyIntime" description="无分析数据" />
      <dept-rectify-intime-analysis v-if="showEmptyCompanyIntime === false" ref = 'companyIntimeAnalysis' chartdomid='companyIntimeChart' :intimeDetail='companyIntimeDataDetail' />
    </van-row>
  </van-row>
</template>

<script>
import DeptRectifyUncompanyMonitor from '@/views/analysis/DeptRectifyAnalysis/DeptRectifyUncompanyMonitor'
import DeptRectifyCompanyMonitor from '@/views/analysis/DeptRectifyAnalysis/DeptRectifyCompanyMonitor'
import DeptRectifyIntimeAnalysis from '@/views/analysis/DeptRectifyAnalysis/DeptRectifyIntimeAnalysis'
import { getDeptRectifyMonitor } from '@/api/analysis'
export default {
  name: 'DeptRectifyMonitor',
  components: { DeptRectifyUncompanyMonitor, DeptRectifyCompanyMonitor, DeptRectifyIntimeAnalysis },
  data () {
    return {
      date: '',
      show: false,
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 11, 31),
      showEmptyUncompany: true,
      showEmptyCompany: true,
      showEmptyCompanyIntime: true,
      showEmptyUnCompanyIntime: true,
      condition: {
        department_path: '',
        begin_date: '',
        end_date: '',
        total: 1
      },
      companyChartData: null,
      companyTableData: null,
      uncompanyChartData: null,
      uncompanyTableData: null,

      companyIntimeData: null,
      uncompanyIntimeData: null,
      companyIntimeDataDetail: null,
      uncompanyIntimeDataDetail: null
    }
  },
  mounted () {
    this.condition.department_path = this.$store.state.authInfo.currentDept.department_path
    this.condition.total = 1
    this.doGetDeptRectifyMonitor()
    console.log(this.$store.state.authInfo.currentDept)
  },
  methods: {
    doBack () {
      this.$router.back()
    },
    doGetDeptRectifyMonitor () {
      let that = this
      getDeptRectifyMonitor(this.condition).then(res => {
        let { data } = res
        if (data[this.$response.CODE] === this.$response.SUCCESS) {
          let retdata = data[this.$response.DATA] ? data[this.$response.DATA] : null
          if (retdata === null) {
            that.showEmptyCompany = true
            that.showEmptyUncompany = true
            that.showEmptyUnCompanyIntime = true
            that.showEmptyCompanyIntime = true
            return
          }

          if (retdata.companyAnalysisDetail.length === 0) {
            that.showEmptyCompany = true
          } else {
            that.showEmptyCompany = false
          }

          if (retdata.uncompanyAnalysisDetail.length === 0) {
            that.showEmptyUncompany = true
          } else {
            that.showEmptyUncompany = false
          }

          if (retdata.uncompanyIntimeAnalysisDetail.length === 0) {
            that.showEmptyUnCompanyIntime = true
          } else {
            that.showEmptyUnCompanyIntime = false
          }

          that.companyChartData = retdata.companyAnalysis[0]
          that.companyTableData = retdata.companyAnalysisDetail
          that.uncompanyChartData = retdata.uncompanyAnalysis[0]
          that.uncompanyTableData = retdata.uncompanyAnalysisDetail

          that.companyIntimeData = retdata.companyIntimeAnalysis[0]
          that.companyIntimeDataDetail = retdata.companyIntimeAnalysisDetail
          that.uncompanyIntimeData = retdata.uncompanyIntimeAnalysis[0]
          that.uncompanyIntimeDataDetail = retdata.uncompanyIntimeAnalysisDetail
          this.$nextTick(() => {
            if (!that.showEmptyUncompany) {
              that.$refs.uncompanyMonitor.renderData()
            }
            if (!that.showEmptyCompany) {
              that.$refs.companyMonitor.renderData()
            }
            console.log('fasdfa' + that.uncompanyIntimeDataDetail.length)
            if (!that.showEmptyUnCompanyIntime) {
              that.$refs.uncompanyIntimeAnalysis.renderData()
            }
          })
        } else {
          this.$toast.fail('获取数据失败：' + data[this.$response.MESSAGE])
        }
      }, errFunc => {
        this.$notify('服务调用失败：' + errFunc.toString())
        console.log(errFunc.toString())
      })
    },
    formatDate (date) {
      return `${date.getMonth() + 1}/${date.getDate()}`
    },
    onConfirm (date) {
      const [start, end] = date
      this.condition.begin_date = this.dateFormat(start, 'yyyy-MM-dd')
      this.condition.end_date = this.dateFormat(end, 'yyyy-MM-dd')
      this.show = false
      this.date = `${this.formatDate(start)} - ${this.formatDate(end)}`
      this.condition.total = 0
      this.doGetDeptRectifyMonitor()
    },
    dateFormat (d, format) {
      var date = {
        'M+': d.getMonth() + 1,
        'd+': d.getDate(),
        'h+': d.getHours(),
        'm+': d.getMinutes(),
        's+': d.getSeconds(),
        'q+': Math.floor((d.getMonth() + 3) / 3),
        'S+': d.getMilliseconds()
      }
      if (/(y+)/i.test(format)) {
        format = format.replace(RegExp.$1, (d.getFullYear() + '').substr(4 - RegExp.$1.length))
      }
      for (var k in date) {
        if (new RegExp('(' + k + ')').test(format)) {
          format = format.replace(RegExp.$1, RegExp.$1.length === 1
            ? date[k] : ('00' + date[k]).substr(('' + date[k]).length))
        }
      }
      return format
    }
  },
  computed: {
    companyIntimeJishi: function () {
      if (this.companyIntimeData === null || this.companyIntimeData.problem_count === 0) {
        return '无数据'
      }
      let result = this.companyIntimeData.intime_complete / (this.companyIntimeData.intime_complete + this.companyIntimeData.timeout_complete + this.companyIntimeData.timeout_uncomplete) * 100
      return result.toFixed(2) + '%'
    },
    uncompanyIntimeJishi: function () {
      if (this.uncompanyIntimeData === null || this.uncompanyIntimeData.problem_count === 0) {
        return '无数据'
      }
      let result = this.uncompanyIntimeData.intime_complete / (this.uncompanyIntimeData.intime_complete + this.uncompanyIntimeData.timeout_complete + this.uncompanyIntimeData.timeout_uncomplete) * 100
      return result.toFixed(2) + '%'
    },
    companyIntimeWancheng: function () {
      if (this.companyIntimeData === null || this.companyIntimeData.problem_count === 0) {
        return '无数据'
      }
      let result = this.companyIntimeData.complete_count / this.companyIntimeData.problem_count * 100
      return result.toFixed(2) + '%'
    },
    uncompanyIntimeWancheng: function () {
      if (this.uncompanyIntimeData === null || this.uncompanyIntimeData.problem_count === 0) {
        return '无数据'
      }
      let result = this.uncompanyIntimeData.complete_count / this.uncompanyIntimeData.problem_count * 100
      return result.toFixed(2) + '%'
    }

  }
}
</script>

<style scoped lang="less">

</style>
